var img_domain_url = 'http://_image.foto21.kr/';

window.onerror = function(message, url, line) { 
//	alert('Url:'+url+'\r\nLine :'+line+'\r\n' +message);
	return true;
}

function swapTopImage(obj, img_name, bl) {
	if (bl) 
		obj.src= img_domain_url + img_name +'.gif';
	else
		obj.src= img_domain_url + img_name +'_1.gif';
}

function stopClickBody(id) {
	var obj = null;
	if (id != null)
		obj = document.getElementById(id);
	else
		obj = document.body;
	if (obj != null)
		obj.ondragstart = obj.onselectstart = obj.oncontextmenu = function() {return false;};
}

var lastShownObj = null;
var lastShownObjID = null;
var lastShownObj3th = null;

function playMsgSound() {
	try {
		var tmpObj = document.createElement('div');
		var file_url = img_domain_url +'dingdong.swf';
		var str = '<OBJECT classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0" width="1" height="1">'
			+ '<param name="movie" value="' + file_url + '">'
			+ '<param name="quality" value="high">'
			+ '<param name="wmode" value="transparent">'
			+ '<embed src="'+ file_url +'" width="1" height="1" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" wmode="transparent"></embed>'
			+ '</OBJECT>';
		tmpObj.innerHTML = str;
		var objs = document.getElementsByTagName('body')[0];
		objs.insertBefore(tmpObj,objs.firstChild);
	} catch(ex) {}
}

function showSubMenu(id, img, obj) {
	if (lastShownObjID != null && lastShownObjID != id)
		hideSubMenu(lastShownObjID);
	var tmpObj = document.getElementById('submenu_'+id);
	if (tmpObj != null) {
		lastShownObj = tmpObj;
		lastShownObj.style.display = 'block';
	}
	lastShownObjID = id;
	if (img != '' && img != null) {
		var imgObj = document.getElementById('imgmenu_'+id);
		if (imgObj != null) {
			if (imgObj.tmpImage == null) {
				imgObj.orgImage = new Image();
				imgObj.orgImage.src = imgObj.src;
				imgObj.tmpImage = new Image();
				imgObj.tmpImage.src = img_domain_url + img +'.gif';
			}
			imgObj.src = imgObj.tmpImage.src;
		}
	}
}

function hideSubMenu(id) {
	if (lastShownObj != null) {
		lastShownObj.style.display = 'none';
		lastShownObj = null;
		lastShownObjID = null;
	}
	var imgObj = document.getElementById('imgmenu_'+id);
	if (imgObj != null && imgObj.orgImage != null) 
		imgObj.src = imgObj.orgImage.src;
}
var stopMenuNavi = false;

function hideAllMenu() {
	if (	lastShownObjID != null)
		hideSubMenu(lastShownObjID);
}
function stopSubMenu(bl) {
	stopMenuNavi = bl;
}

function show3thMenu(id1, id2, no) {
	var divObj = document.getElementById('submenu_'+id1 +id2);
	if (lastShownObj3th != null)
		lastShownObj3th.style.display = 'none';
	if (divObj != null) {
		lastShownObj3th = divObj;
		lastShownObj3th.style.display = 'block';
	}
}

function cumulativeOffset(_element) {
	var valueT = 0, valueL = 0;
	do {
		valueT += _element.offsetTop  || 0;
		valueL += _element.offsetLeft || 0;
		_element = _element.offsetParent;
	} while (_element);
	return [valueL, valueT];
}

var userLayerObj = null;
var userLayerLastID = null;
var userLayerLastObj = null;
function showUserLayer(obj, ids) {
	if (userLayerLastObj == obj)
		hideUserLayer();
	else {
		if (	userLayerObj == null) {
			userLayerObj = document.createElement('div');
			userLayerObj.style.position = 'absolute';
			userLayerObj.className = 'dropLayer';
			userLayerObj.style.left = '0px';
			userLayerObj.style.top = '0px';
			userLayerObj.style.zIndex = '200';
			var targetObj=document.getElementsByTagName("body")[0];
			if (targetObj != null) {
				targetObj.appendChild(userLayerObj);
				var html = [];
				html.push('<table border=0>');
				html.push('<tr><td align=right><a href="javascript:hideUserLayer()">닫기</a></td></tr>');
				html.push('<tr><td><a href="javascript:callUserCmd(\'info\')">회원정보 보기</a></td></tr>');
				html.push('<tr><td><a href="javascript:callUserCmd(\'serarchid\')">아이디로 검색</a></td></tr>');
				html.push('<tr><td><a href="javascript:callUserCmd(\'post\')">글 목록 보기</a></td></tr>');
				html.push('<tr><td><a href="javascript:callUserCmd(\'scrap\')">글 모음 보기</a></td></tr>');
				html.push('<tr><td><a href="javascript:callUserCmd(\'msg\')">쪽지 보내기</a></td></tr>');
				html.push('</table>');
				userLayerObj.innerHTML = html.join('\r\n');
			}
		}
		var pos = cumulativeOffset(obj);
		userLayerObj.style.display = 'block';
		userLayerObj.style.left = (pos[0]+obj.offsetWidth) +'px';
		userLayerObj.style.top = pos[1] +'px';
		userLayerLastID = ids;
		userLayerLastObj = obj;
	}
}

function hideUserLayer() {
	if (userLayerObj != null)
		userLayerObj.style.display = 'none';
	userLayerLastID = null;
	userLayerLastObj = null;
}

function sendMsg(user_id) {
	if (checkLogin())
		showPopup('/members/' +user_id +'.html', {mode : 'msgFormSend'});
}

function keepMsg(user_id, no) {
	new cafen.Ajax({msguid : no, mode : 'xmlMsgKeep'}, ajaxCallEnd, '/members/'+user_id +'.html', 'post');
}

function deleteMsg(user_id, no) {
	new cafen.Ajax({msguid : no, mode : 'xmlDeleteMsg'}, ajaxCallEnd, '/members/'+user_id +'.html', 'post');
}

function checkSearch(form) {
	if (form.sk.value == '') {
		alert('검색어를 입력하여 주십시오.');
		form.sk.focus();
		return false;
	} else 
		return true;
}

function changeURLObject(obj, base_url) {
	document.location.href = base_url +'?'+obj.getAttribute('name') +'=' +obj[obj.selectedIndex].value;
}



var loginPopup = null;

function showLoginPopup() {
	if (loginPopup == null) 
		loginPopup = new cafen.XFaceBox({className :'', tplName : '', attribute :{className : 'popupLayer'}, hideClose:true, title : '로그인',ajax_url : '/common/login.html', ajax_data : {mode :'xmlLoginForm'}, style : {zIndex: 100}});
	loginPopup.onLoad();
}

function checkSavePW(obj) {
	if (obj.checked) {
		var txt = [];
		txt.push('브라우즈를 닫더라도 로그인이 계속 유지될 수 있습니다.');
		txt.push('자동로그인 기능을 사용할 경우 다음 접속 부터는 로그인을 하실 필요가 없습니다.');
		txt.push('단, 게임방, 학교 등 공공장소에서 이용 시 개인정보가 유출될 수 있으니 꼭 로그아웃을 해 주세요.');
		if (!confirm(txt.join('\r\n\r\n')))
			obj.checked = false;
	} else
		return true;
}

function checkLogin(obj) {
	if (document.cookie.indexOf("CLTCOOKINFO") == -1) {
		var txt = [];
		txt.push('해당 기능은 로그인을 하셔야 사용하실 수 있습니다.');
		txt.push('지금 로그인 하시겠습니까?');
		if (confirm(txt.join('\r\n\r\n')))
			showLoginPopup();
		if (obj != null)
			obj.blur();
		return false;
	} else 
		return true;
}

function showUserInfo (user_id) {
	if (checkLogin())
		showPopup('/members/' +user_id +'.html', {mode:'xmlShowInfo'})
}

function callUserCmd(cmd) {
	switch(cmd) {
		case 'info' :
			showUserInfo(userLayerLastID)
			break;
		case 'msg' :
			sendMsg(userLayerLastID);
			break;
		case 'post' :
			goUrl('/members/' +userLayerLastID +'.html?mode=postForm');
			break;
		case 'scrap' :
			goUrl('/members/' +userLayerLastID +'.html?mode=mypostForm');
			break;
		case 'serarchid' :
			goUrl('/common/search.html?so=I&sk=' +userLayerLastID);
			break;
		default : 
			break;
	}
	hideUserLayer();
}

var postLayerObj = null;
var postLayerLastID = null;
function showPostInfo(obj, board_id, post_no, options) {
	if (postLayerLastID != null && postLayerLastID.board_id == board_id && postLayerLastID.uid == post_no) 
		hidePostInfo();
	else {
		if (	postLayerObj == null) {
			postLayerObj = document.createElement('div');
			postLayerObj.style.position = 'absolute';
			postLayerObj.className = 'dropLayer';
			postLayerObj.style.left = '0px';
			postLayerObj.style.top = '0px';
			var targetObj=document.getElementsByTagName("body")[0];
			if (targetObj != null) {
				targetObj.appendChild(postLayerObj);
				var html = [];
				html.push('<table border=0 width=90>');
				html.push('<tr><td style="text-align:right"><a href="javascript:hidePostInfo()">닫기</a></td></tr>');
				if (options.charAt(0) == 'O')
					html.push('<tr><td><a href="javascript:callPostCmd(\'good\')">추천</a></td></tr>');
				if (options.charAt(1) == 'O')
					html.push('<tr><td><a href="javascript:callPostCmd(\'bad\')">비추천</a></td></tr>');
				if (options.charAt(2) == 'O')
					html.push('<tr><td><a href="javascript:callPostCmd(\'scrap\')">스크랩</a></td></tr>');
				if (options.charAt(3) == 'O')
					html.push('<tr><td><a href="javascript:callPostCmd(\'report\')">신고하기</a></td></tr>');
				html.push('<tr><td><a href="javascript:callPostCmd(\'print\')">인쇄하기</a></td></tr>');
				html.push('</table>');
				postLayerObj.innerHTML = html.join('\r\n');
			}
		}
		var pos = cumulativeOffset(obj);
		postLayerObj.style.display = 'block';
		postLayerObj.style.left = (pos[0]+obj.offsetWidth -100) +'px';
		postLayerObj.style.top = (pos[1]+obj.offsetHeight) +'px';
		postLayerLastID = {board_id : board_id, uid : post_no};
	}
}

function hidePostInfo() {
	if (postLayerObj != null)
		postLayerObj.style.display = 'none';
	postLayerLastID = null;
}

function callPostCmd(cmd) {
	switch(cmd) {
		case 'print' :
			window.print();
			break;
		case 'good' :
			postAjax(postLayerLastID.board_id, postLayerLastID.uid, 'goodAjax');
			break;
		case 'bad' :
			postAjax(postLayerLastID.board_id, postLayerLastID.uid, 'badAjax');
			break;
		case 'scrap' :
			scrapPost(postLayerLastID.board_id, postLayerLastID.uid);
			break;
		case 'report' :
			reportPost(postLayerLastID.board_id, postLayerLastID.uid);
			break;
		default :
			alert(cmd);
			break;	
	}
	hidePostInfo();
}

function scrapPost(board_id, no) {
	if (checkLogin())
		postAjax(board_id, no, 'scrapAjax');
}

function reportPost(board_id, no) {
	if (checkLogin())
		showPopup('/'+board_id +'/'+ no +'.html', {mode : 'reportForm'});
}

function goodPost(board_id, no) {
	postAjax(board_id, no, 'goodAjax');
}

function badPost(board_id, no) {
	postAjax(board_id, no, 'badAjax');
}

function postAjax(board_id, no, mode) {
	new cafen.Ajax({uid : no, mode : mode}, ajaxCallEnd, '/'+board_id +'/'+no+'.html', 'post');
}

function copyUrl(obj) {
	try {
		var doc = document.body.createTextRange();
		doc.moveToElementText(obj);
		doc.select();
		doc.execCommand('copy');
		alert('주소가 복사되었습니다.\r\n붙여넣기 하시면 됩니다.');
	} catch(ex) {
		return ;
	}
}

function ajaxCallEnd(channel) {
	submitProcessing = false;	
	var nextURL = null, msg = null, msgtitle = null, msgsubtitle = null;
	if (channel.checkMsg()) 
		msg = channel.getMsg().title;
	if (channel.getNode) {
		nextURL = channel.getNode('nexturl');
		msgtitle = channel.getNode('msgtitle');
		msgsubtitle = channel.getNode('msgsubtitle');
	}
	if (nextURL == 'reload') {
		if (msg != null) 
			alert(msg.replace(/<br\/?[^>]+>/gi, '\r\n').replace(/<\/?[^>]+>/gi, ''));
		top.document.location.reload();
	} else if (nextURL != null && nextURL != '') {
		if (msg != null || msgsubtitle != '') {
			var txt = [];
			if (msgtitle != null)
				txt.push('※ '+msgtitle);
			if (msgtitle != null)
				txt.push('   '+msgsubtitle);
			if (msg != null)
				txt.push(msg);
			alert(txt.join('\r\n\r\n').replace(/<br\/?[^>]+>/gi, '\r\n').replace(/<\/?[^>]+>/gi, ''));
		}
		top.document.location.href = nextURL;
	} else if (msg != null || msgsubtitle != null) {
		var txt = [];
		txt.push('<table border=0 width=200px>');
		if (msgtitle != null && msgtitle != '') 
			txt.push('<tr><td class="title">'+msgtitle+'</td></tr>');
		if (msg != null && msg != '') {
			if (msgsubtitle != null && msgsubtitle != '') 
				txt.push('<tr><td class="subtitle">'+msgsubtitle+'</td></tr>');
			if (msg != null && msg != '')
				txt.push('<tr><td style="padding:5px;text-align:center;line-height:200%" nowrap>'+msg+'</td></tr>');
		} else if (msgsubtitle != null && msgsubtitle != '') 
			txt.push('<tr><td style="padding:5px;text-align:center;line-height:200%" nowrap>'+msgsubtitle+'</td></tr>');
		txt.push('</table>');
		showPopupHtml(txt.join(''));
	}
}


function goUrl(theUrl) {
	document.location.href = theUrl;
}

function goUrlForm(form, options) {
	var theUrl = form.action || document.location.pathname ;
	var params = [];
	for(var idx in options) {
		params.push(idx+'='+encodeURIComponent(options[idx]));
	}
	if (params.length > 0)
		theUrl += '?' + params.join('&'); 
	goUrl(theUrl);
}

function goUrlButton(obj, mode, uid) {
	var theUrl = obj.getAttribute('href') || document.location.pathname;
	if (theUrl.indexOf('?') == -1) {
		if (mode != null) {
			theUrl += '?mode=' + encodeURIComponent(mode);
			if (uid != null) 
				theUrl += '&uid=' + encodeURIComponent(uid);
		} else
			theUrl += getDefaultParam();
	}
	goUrl(theUrl);
}

function getDefaultParam() {
	var theURL = document.location.href;
	var urlVal = theURL.split('?');
	var mainUrl = urlVal[0];
	var getDataHash = {};
	if (urlVal.length > 1 && urlVal[1] != '') {
		urlVal[1] = urlVal[1].split('#')[0];
		var getDatas = urlVal[1].split('&');
		for(var i = 0; i < getDatas.length; i++) {
			var keyVal = 	getDatas[i].split('=');
			if (keyVal.length > 1 && keyVal[0] != '' && keyVal[1] != '' && (keyVal[0] == 'cline' || keyVal[0] == 'so' || keyVal[0] == 'sk'))
				getDataHash[keyVal[0]] = keyVal[1];
		}
	}
	var newData = [];
	for(var idx in getDataHash) 
		newData.push(idx +'=' + getDataHash[idx]);
	if (newData.length > 0) 
		return '?' + newData.join('&');
	else
		return '';
}

function goXmlButtonSimple(mode, uid, suid, href) {
	goXmlButton({href : href}, {mode: mode, uid : uid || '', suid : suid || ''});
}

function goXmlButton(obj, options) {
	switch(options.mode) {
		case 'xmlTail